import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import java.util.HashMap;

// this class contains the control elements of GUI
class Control
{
	private JTextArea messagePanel;
	private JTextArea shipsCountPanel;
	private JPanel ctnr;
	private Battle battle = Battle.getInstance();
	private ArrayList<JButton> controlButtons = new ArrayList<JButton>();
	private static Control instance;

	public Control()
	{
		setCtnr();
	}
	
	public void menu()
	{
		//setCtnr();
		clearCtnr();
		
		//ctnr.setLayout(null);
		ctnr.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		GUI.window.getContentPane().add(ctnr);
		
		JButton startBattleBtn = new JButton("Start battle");
		startBattleBtn.addActionListener(new MyEventHandler("prepareBattleField"));
		startBattleBtn.setPreferredSize(new Dimension(100, 30));
		c.fill = GridBagConstraints.VERTICAL;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 3;
		ctnr.add(startBattleBtn, c);
		
		JButton statisticsBtn = new JButton("Statistics");
		statisticsBtn.setPreferredSize(new Dimension(100, 30));
		c.fill = GridBagConstraints.VERTICAL;
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 3;
		c.insets = new Insets(10,0,0,0);
		ctnr.add(statisticsBtn, c);
		
		JButton exitBtn = new JButton("Exit");
		exitBtn.setPreferredSize(new Dimension(100, 30));
		c.fill = GridBagConstraints.VERTICAL;
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 3;
		ctnr.add(exitBtn, c);
		
		GUI.window.validate();
		
	}
		
	public void prepareBattle()
	{
		if (Battle.isFinished()) { battle.setIsFinished(false); }
	
		battle.areas.get(0).getMap().reloadAreaCells();
		battle.areas.get(0).getMap().enableLocatingMode();
		battle.areas.get(0).reset();
		battle.areas.get(1).getMap().reloadAreaCells();
		battle.areas.get(1).getMap().enableLocatingMode();
		battle.areas.get(1).reset();
		clearCtnr();
		ctnr.setLayout(null);
		//ctnr.setLayout(new BorderLayout());
		GUI.window.getContentPane().add(ctnr);
		//GUI.window.getContentPane().add(ctnr, BorderLayout.PAGE_START);
		//ctnr.setVisible(true);
		Insets insets = ctnr.getInsets();
		
		JButton btn1 = new JButton("Locate random");
		ctnr.add(btn1);
		btn1.setBounds(50 + insets.left, 470 + insets.top, 120, 30);
		btn1.addActionListener(new MyEventHandler("locatingRandom"));
		ctnr.add(btn1);
		controlButtons.add(btn1);
		
		JButton btn2 = new JButton("Finish locating ships");
		btn2.setBounds(180 + insets.left, 470 + insets.top, 170, 30);
		btn2.addActionListener(new MyEventHandler("startBattle"));
		ctnr.add(btn2);
		controlButtons.add(btn2);
		
		JButton btn3 = new JButton("back");
		ctnr.add(btn3);
		btn3.setBounds(360 + insets.left, 470 + insets.top, 90, 30);
		btn3.addActionListener(new MyEventHandler("menu"));
		ctnr.add(btn3);
		controlButtons.add(btn3);
		
		battle.areas.get(0).getMap().render(ctnr);
		
		GUI.window.validate();
	}
	
	public void messagePanel()
	{
		messagePanel = new JTextArea(5, 20);
		messagePanel.setEditable(false);
	
		messagePanel.setForeground(Color.white);
		Insets insets = ctnr.getInsets();
		messagePanel.setBounds(485 + insets.left, insets.top + 50, 170, 150);
		messagePanel.setBackground(Color.gray);
		messagePanel.setOpaque(true);
		messagePanel.setLineWrap(true);
		messagePanel.setMargin(new Insets(10,10,10,10)); 
		ctnr.add(messagePanel);
		
	}
	
	public void shipsCountPanel()
	{
		shipsCountPanel = new JTextArea(5, 20);
		shipsCountPanel.setEditable(false);
		
		shipsCountPanel.setForeground(Color.white);
		Insets insets = ctnr.getInsets();
		shipsCountPanel.setBounds(485 + insets.left, insets.top + 230, 170, 150);
		shipsCountPanel.setBackground(Color.gray);
		shipsCountPanel.setOpaque(true);
		shipsCountPanel.setLineWrap(true);
		shipsCountPanel.setMargin(new Insets(10,10,10,10)); 
		ctnr.add(shipsCountPanel);
	}
	
	public void startNewBattleButton()
	{
		JButton startNewBattleButton = new JButton("Start a new battle");

		//startNewBattleButton.setVisible(false);
			
		Insets insets = ctnr.getInsets();
			
		startNewBattleButton.setBounds(485 + insets.left, insets.top + 410, 170, 40);
		startNewBattleButton.setMargin(new Insets(10, 10, 10, 10)); 
		
		startNewBattleButton.addActionListener(new MyEventHandler("prepareBattleField"));
		
	    ctnr.add(startNewBattleButton);
		
		GUI.window.validate();
	}
	
	public void clearCtnr()
	{
		ctnr.removeAll();
		ctnr.repaint();
		ctnr.setSize(600, 600);
	}
	
	public void simulateButtonClick()
	{
		Battle battle = Battle.getInstance();
		Area area = battle.areas.get(0);
	
	
		String coord = area.findProperAim();
		MyButton btnRand = area.getMap().getButtonByCoord(coord);
				
		btnRand.doClick();
	}
	
	public void startBattle()
	{	
		GUI.window.setSize(1200, 600);
		//ctnr.remove(messagePanel); 
		//ctnr.remove(shipsCountPanel); 
		startNewBattleButton();
		
		for (JButton btn : controlButtons)
		{
			ctnr.remove(btn);
		}
		
		Battle battle = Battle.getInstance();
		
		battle.areas.get(1).locateShipsRandom();
		//battle.areas.get(1).getMap().locateShips(battle.areas.get(1).getCells());
		battle.areas.get(0).getMap().block();
		battle.areas.get(1).getMap().enableWarMode();
		battle.areas.get(1).getMap().render(ctnr);
		ctnr.setSize(1200, 600);
		ctnr.revalidate();
		GUI.window.getContentPane().validate();
		GUI.window.getContentPane().repaint();
                
                clearInfoPanes();
		
		//for (int i = 0; i < 10; i++)	
                
                battle.stats.startTimer();
	}
	
	public void displayShipsQty(int[] shipsByRang)
	{
		shipsCountPanel.setText(null);
		shipsCountPanel.append("1 deck: " + shipsByRang[0] + "\n");
		shipsCountPanel.append("2 deck: " + shipsByRang[1] + "\n");
		shipsCountPanel.append("3 deck: " + shipsByRang[2] + "\n");
		shipsCountPanel.append("4 deck: " + shipsByRang[3] + "\n");
	}
        
        public void displayStats(HashMap stats)
        {
            shipsCountPanel.setText(null);
            shipsCountPanel.append("Statistics: \n");
            shipsCountPanel.append("Time: " + stats.get("time") + "\n");
            shipsCountPanel.append("Turns: " + stats.get("turns") + "\n");
            shipsCountPanel.append("Winner: " + stats.get("winner") + "\n");
        }        
        
        private void clearInfoPanes()
        {
            shipsCountPanel.setText(null);
            shipsCountPanel.setText(null);
        }
	
	public void displayMessage(String text)
	{
		messagePanel.setText(text);
		
		/*Timer timer = new Timer();
		timer.schedule(new TimerTask() 
		{
			@Override
			public void run() 
			{
				messagePanel.setText(null);
			}
		}, 3000);*/
	}
	
	public void switchArea()
	{		
		if (battle.areaUnderAttack == 0)
		{
			battle.areaUnderAttack = 1;
		}
		else
		{
			battle.areaUnderAttack = 0;
		}
		
		battle.areas.get(0).getMap().block();
		battle.areas.get(1).getMap().block();

		battle.areas.get(battle.areaUnderAttack).getMap().enableWarMode();
	}
	
	public static Control getInstance()
	{
		if (Control.instance == null)
		{
			Control.instance = new Control();
		}
		
		return Control.instance;
	}
	
	private void setCtnr()
	{
		ctnr = new JPanel();
		ctnr.setSize(1200, 600);
		GUI.window.getContentPane().add(ctnr);
		
	}
}